import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Manager',
      component: () => import('@/views/Manager.vue'),
      redirect: '/login',
      children: [
        { path: 'home', name: 'Home', component: () => import('@/views/manager/Home.vue')},
        { path: 'userManager', name: 'UserManager', component: () => import('@/views/manager/UserManager.vue')},
        { path: 'gymManager', name: 'GymManager', component: () => import('@/views/manager/GymManager.vue')},
        { path: 'gameInfoManager', name: 'GameInfoManager', component: () => import('@/views/manager/GameInfoManager.vue')},
        { path: 'ticketManager', name: 'TicketManager', component: () => import('@/views/manager/TicketManager.vue')},
        { path: 'ticketRecordManager', name: 'TicketRecordManager', component: () => import('@/views/manager/TicketRecordManager.vue')},
        { path: 'liuYanManager', name: 'LiuYanManager', component: () =>  import('@/views/manager/LiuYanManager.vue')},
        { path: 'person', name: 'Person', component: () =>  import('@/views/manager/Person.vue')}

      ]
    },
    {path: '/login', name: 'Login', component: () => import('@/views/Login.vue'),},
    {path: '/register', name: 'Register', component: () => import('@/views/Register.vue'),}
  ]
})

export default router
//前端在跳转页面前，应当也要校验登录状态信息
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    let token = localStorage.getItem('token')
    if (token === null || token === '') {
      next('/login')
    } else {
      next()
    }
  }
})